/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.JavaFX.ListView;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.stage.Stage;
import java.text.NumberFormat;
import javafx.scene.control.ListCell;
import javafx.scene.paint.Color;

/**
 *
 * @author kylin
 */
public class TestListView1 extends Application{
    
    public Parent createContent() {
         final ListView<Number> listView = new ListView<>();
        listView.setItems(FXCollections.<Number>observableArrayList(
                100.00, -12.34, 33.01, 71.00, 23000.00, -6.00, 0, 42223.00, -12.05, 500.00,
                430000.00, 1.00, -4.00, 1922.01, -90.00, 11111.00, 3901349.00, 12.00, -1.00, -2.00,
                15.00, 47.50, 12.11
 
        ));
 
        listView.setCellFactory((ListView<java.lang.Number> list) -> new MoneyFormatCell());
 
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
        return listView;
    }
 
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene(new Scene(createContent()));
        primaryStage.show();
    }
 
    /**
     * Java main for when running without JavaFX launcher
     */
    public static void main(String[] args) {
        launch(args);
    }
}
class MoneyFormatCell extends ListCell<Number> {
    @Override
    public void updateItem(Number item, boolean empty) {
        super.updateItem(item, empty);
 
        // format the number as if it were a monetary value using the
        // formatting relevant to the current locale. This would format
        // 43.68 as "$43.68", and -23.67 as "($23.67)"
        setText(item == null ? "" : NumberFormat.getCurrencyInstance().format(item));
 
        if (item != null) {
            double value = item.doubleValue();
 
            setTextFill(value == 0 ? Color.BLACK
                    : value < 0 ? Color.RED : Color.GREEN);
        }
    }
}